/** ZDD Node Size and No of Files (Figure 4-1)
 * 
 * 
 * @author Yong Boon, Lim (yongboon.lim@gmail.com)
 */

package eval;

import java.io.File;
import java.util.ArrayList;
import java.util.Map;

import core.NNZDDBasic;


import util.DocUtils;
import util.FileFinder;

public class EvalNNZDDBaisc extends NNZDDBasic {
	
	int _totNoOfStemWord = 0;

	public EvalNNZDDBaisc(String trainDataDir) {
		super(trainDataDir);
		_nnDescShort = "NN-ZDD-HD-LUC";
		_nnDescLong = "Nearest Neighbor with ZDD Hamming Distance (Using Lucene to retrieve doc)";
	}
	
	// Convert topic to ZDD
	@Override
	public int topic2Zdd(File file) {
		String fileContent = DocUtils.ReadFile(file);
		Map<Object, Double> wordCount = DocUtils.ConvertToFeatureMapFilter(fileContent);
		_totNoOfStemWord = _totNoOfStemWord + wordCount.size();
		int zddTopic = _zdd.base();

		for (Map.Entry<Object, Double> me : wordCount.entrySet()) {
			// If the stem word have not been created as variable
			if (!_stemVarDict.containsKey(me.getKey())) {
				int zddVar = _zdd.createVar();
				_stemVarDict.put((String) me.getKey(), zddVar);
			}
			zddTopic = changeWith(zddTopic, _stemVarDict.get(me.getKey()));
		}
		return zddTopic;
	}

	// Union all the topics' ZDD
	@Override
	protected int zddTopicUnion() {
		ArrayList<File> files = FileFinder.GetAllFiles( _trainDataDir, "", true);
		int zddTopic;
		int zddTmp = _zdd.base();
		int numFileProcessed = 0;

		System.out.println("numFileProcessed, zddNodeSize, totNoOfStemWord@Doc, totUniqueStemWord");
		for (File f : files) {
			zddTopic = topic2Zdd(f);
			zddTmp = unionWith(zddTmp, zddTopic);
			numFileProcessed++;
			System.out.println(numFileProcessed + ", " + _zdd.nodeCount(zddTmp) + ", " + _totNoOfStemWord +", " + _stemVarDict.size());
			
			if (numFileProcessed%1000 == 0) {
				System.out.println("File Processed: " + numFileProcessed);
			}
		}

		return zddTmp;
	}
	

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// Compare the ZDD Node Size and Document Size
		String trainDataDir = "data/more_newsgroups/alt.atheism";
		EvalNNZDDBaisc nnZDD = new EvalNNZDDBaisc(trainDataDir);
		nnZDD.init();
		nnZDD.build();
	}

}
